﻿Imports System.Text
Imports D2L.Extensibility.AuthSdk
Imports RestSharp
Imports D2L.Extensibility.AuthSdk.Restsharp

''' <summary>
''' Referente aos métodos utilizados na ferramenta https://apitesttool.desire2learnvalence.com
''' </summary>
''' <remarks></remarks>
<TestClass()>
Public Class ApiTestToolTeste

    'Ferramenta de teste
    'https://apitesttool.desire2learnvalence.com
    'CONFIGURAÇÃO PARA TESTE
    '	Host: lms.valence.desire2learn.com
    '	Port: 443
    '	App ID: G9nUpvbZQyiPrk3um2YAkQ
    '	App Key: ybZu7fm_JKJTFwKEHfoZ7Q
    '	User ID: F1vg52cjNkgbBnbcfKovEA
    '	User Key: Q8FGD5Zl8DJ8KtR1ZE4ujQ

    Protected Const APP_ID As String = "G9nUpvbZQyiPrk3um2YAkQ"
    Protected Const APP_KEY As String = "ybZu7fm_JKJTFwKEHfoZ7Q"

    Private _authObj As Authenticate.Authenticate = New Authenticate.Authenticate()

    Protected Const USER_ID As String = "KOOxZJijDZ3k7cnre-fHFK"
    Protected Const USER_KEY As String = "bCdB88XbvWwpeL6JAQVWNF"
    'Protected Const USER_ID As String = "F1vg52cjNkgbBnbcfKovEA" 'DEPRECIADO 12/05/14
    'Protected Const USER_KEY As String = "Q8FGD5Zl8DJ8KtR1ZE4ujQ" 'DEPRECIADO 12/05/14


    Protected Const LMS_URL As String = "lms.valence.desire2learn.com"
    Protected Const LMS_URL_HTTPS As String = "https://" & ApiTestToolTeste.LMS_URL
    Protected Const LMS_PORTA As Integer = 443
    Protected Const LP_VERSION As String = "1.2"

    Protected method As Authenticate.Service

    'http://docs.valence.desire2learn.com/res/user.html#User.WhoAmIUser
    Protected Const ROUTE_USERS_WHOAMI As String = "/d2l/api/lp/" + ApiTestToolTeste.LP_VERSION + "/users/whoami"

    <TestMethod()>
    Public Sub users_whoami()

        Dim _servico As String = ApiTestToolTeste.ROUTE_USERS_WHOAMI
        Dim _valenceAppContext As ID2LAppContext
        Dim _valenceHost As HostSpec
        Dim _valenceUserContext As ID2LUserContext
        Dim _appFactory As D2LAppContextFactory
        Dim _client As RestClient
        Dim _request As RestRequest
        Dim _authenticator As ValenceAuthenticator
        Dim _response As IRestResponse(Of User.WhoAmIUser) '----

        _appFactory = New D2LAppContextFactory()
        _request = New RestRequest(_servico)
        _client = New RestClient(ApiTestToolTeste.LMS_URL_HTTPS)
        _valenceAppContext = _appFactory.Create(ApiTestToolTeste.APP_ID, ApiTestToolTeste.APP_KEY)
        _valenceHost = New HostSpec("https", ApiTestToolTeste.LMS_URL, ApiTestToolTeste.LMS_PORTA)
        _valenceUserContext = _valenceAppContext.CreateUserContext(ApiTestToolTeste.USER_ID, ApiTestToolTeste.USER_KEY, _valenceHost)

        _authenticator = New ValenceAuthenticator(_valenceUserContext)
        _authenticator.Authenticate(_client, _request)

        _response = _client.Execute(Of User.WhoAmIUser)(_request) '----

        Console.WriteLine("{0}{1}", ApiTestToolTeste.LMS_URL_HTTPS, _servico)
        Console.WriteLine("content: {0}", _response.Content)

        Assert.IsNotNull(_response.Data)

    End Sub

End Class
